package com.warom.sdg.model;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * PLC数据实体类
 * 用于存储PLC采集的数据
 */
public class PlcDataEntity {
    
    private int id;               // 自增主键
    private int deviceId;         // 设备ID，关联到设备表
    private String name;          // 数据点名称
    private String address;       // Modbus地址
    private String type;          // 数据类型（温度、湿度等）
    private String value;         // 数据值
    private String unit;          // 数据单位（℃、%、kg等）
    private long timestamp;       // 时间戳
    private String timeString;    // 时间字符串表示，格式：yyyy-MM-dd HH:mm:ss
    private boolean alarmState;   // 报警状态，true表示报警，false表示正常

    private boolean runningState; //运行状态, true 表示正常, false 表示异常

    private String englishName;  //测点的唯一编号
    
    // 时间格式化器
    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
    
    /**
     * 默认构造函数
     */
    public PlcDataEntity() {
        this.alarmState = false;  // 默认不报警
    }
    
    /**
     * 设置时间戳，同时自动生成时间字符串
     * @param timestamp 时间戳
     */
    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
        this.timeString = formatTimestamp(timestamp);
    }
    
    /**
     * 格式化时间戳为字符串
     * @param timestamp 时间戳
     * @return 格式化后的时间字符串
     */
    private String formatTimestamp(long timestamp) {
        return dateFormat.format(new Date(timestamp));
    }
    
    /**
     * 更新时间字符串
     * 根据当前时间戳更新时间字符串
     */
    public void updateTimeString() {
        if (timestamp > 0) {
            this.timeString = formatTimestamp(timestamp);
        }
    }
    
    // Getters and Setters
    
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    public int getDeviceId() {
        return deviceId;
    }
    
    public void setDeviceId(int deviceId) {
        this.deviceId = deviceId;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getAddress() {
        return address;
    }
    
    public void setAddress(String address) {
        this.address = address;
    }
    
    public String getType() {
        return type;
    }
    
    public void setType(String type) {
        this.type = type;
    }
    
    public String getValue() {
        return value;
    }
    
    public void setValue(String value) {
        this.value = value;
    }
    
    public String getUnit() {
        return unit;
    }
    
    public void setUnit(String unit) {
        this.unit = unit;
    }
    
    public long getTimestamp() {
        return timestamp;
    }
    
    public String getTimeString() {
        // 如果时间字符串为空但时间戳存在，生成时间字符串
        if (timeString == null && timestamp > 0) {
            timeString = formatTimestamp(timestamp);
        }
        return timeString;
    }
    
    public void setTimeString(String timeString) {
        this.timeString = timeString;
    }
    
    /**
     * 获取报警状态
     * @return 报警状态，true表示报警，false表示正常
     */
    public boolean isAlarmState() {
        return alarmState;
    }
    
    /**
     * 设置报警状态
     * @param alarmState 报警状态，true表示报警，false表示正常
     */
    public void setAlarmState(boolean alarmState) {
        this.alarmState = alarmState;
    }

    public boolean isRunningState() {
        return runningState;
    }

    public void setRunningState(boolean runningState) {
        this.runningState = runningState;
    }

    public String getEnglishName() {
        return englishName;
    }

    public void setEnglishName(String englishName) {
        this.englishName = englishName;
    }

    @Override
    public String toString() {
        return "PlcDataEntity{" +
                "id=" + id +
                ", deviceId=" + deviceId +
                ", name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", type='" + type + '\'' +
                ", value='" + value + '\'' +
                ", unit='" + unit + '\'' +
                ", timestamp=" + timestamp +
                ", timeString='" + getTimeString() + '\'' +
                ", alarmState=" + alarmState +
                '}';
    }
} 